package cn.tedu.ivos.dictOption.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.dict.mapper.DictMapper;
import cn.tedu.ivos.dict.pojo.dto.DictQuery;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.dictOption.mapper.DictOptionMapper;
import cn.tedu.ivos.dictOption.pojo.dto.DictOptionQuery;
import cn.tedu.ivos.dictOption.pojo.dto.SaveDictOptionDTO;
import cn.tedu.ivos.dictOption.pojo.entity.DictOption;
import cn.tedu.ivos.dictOption.pojo.vo.DictOptionVO;
import cn.tedu.ivos.dictOption.service.DictOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author liyuehui
 * @time 2024/7/13 10:22
 */
@Slf4j
@Service
public class DictOptionServiceImpl implements DictOptionService {
    @Autowired
    private DictOptionMapper dictOptionMapper;
    @Autowired
    private DictMapper dictMapper;

    /**
     * 查询字典项列表
     * @param dictOptionQuery
     * @return
     */
    @Override
    public List<DictOptionVO> select(DictOptionQuery dictOptionQuery) {
        return dictOptionMapper.select(dictOptionQuery);
    }

    // todo


    /**
     * 根据字典编码查询字典项列表
     * @param code
     * @return
     */
    @Override
    public List<DictOptionVO> selectDictOptionByCode(String code) {
        DictQuery dictQuery = new DictQuery();
        dictQuery.setCode(code);
        List<DictVO> dictList = dictMapper.selectByCode(dictQuery);
        if (dictList.size() == 0){
            throw new ServiceException(StatusCode.DICT_CODE_IS_NOT_EXISTS);
        }
        return dictOptionMapper.selectDictOptionByCode(code);
    }

    /**
     * 保存字典项
     * @param dictOptionDTO
     * @return
     */
    @Override
    public int save(SaveDictOptionDTO dictOptionDTO) {
        DictOption dictOption = new DictOption();
        BeanUtils.copyProperties(dictOptionDTO, dictOption);
        if (dictOptionDTO.getId() == null){
            // 新增
            log.debug("新增字典项");
            DictOptionQuery dictOptionQuery = new DictOptionQuery();
            dictOptionQuery.setValue(dictOptionDTO.getValue());
            if (dictOptionMapper.selectDictOption(dictOptionQuery) != null){
                throw new ServiceException(StatusCode.DICT_OPTION_VALUE_ALREADY_EXISTS);
            }
            dictOption.setCreateTime(new Date());
            log.info("新增字典项:{}", dictOption);
            int result = dictOptionMapper.insert(dictOption);
            if (result != 1){
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
            return result;
        }else {
            //修改
            dictOption.setUpdateTime(new Date());
            log.info("修改字典项:{}", dictOption);
            int result = dictOptionMapper.update(dictOption);
            if (result != 1) {
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
            return result;
        }
    }

    /**
     * 根据dictOptionQuery查询字典项
     * @param dictOptionQuery
     * @return DictOptionVO
     */
    @Override
    public DictOptionVO selectDictOption(DictOptionQuery dictOptionQuery) {
        DictOptionVO dictOption = dictOptionMapper.selectDictOption(dictOptionQuery);
        if (dictOption == null){
            throw new ServiceException(StatusCode.DICT_OPTION_NOT_EXISTS);
        }
        return dictOption;
    }

    /**
     * 删除字典项
     * @param id
     * @return int
     */
    @Override
    public int delete(Long id) {
        // todo 查询该字典项是否被绑定使用, 如果被绑定使用则不能删除
        int result = dictOptionMapper.delete(id);
        if (result != 1){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        return result;
    }
}
